//
//  loginPassWordViewController.m
//  HJB_project
//
//  Created by jiezsoft.com on 14-11-12.
//  Copyright (c) 2014年 jiezsoft.com. All rights reserved.
//

#import "AddOrEditAddressViewController.h"
#import "TheLoginViewController.h"
#import "HZAreaPickerView.h"
#import "AddAddressView.h"
@interface AddOrEditAddressViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate ,UINavigationControllerDelegate,HZAreaPickerDelegate,AddAddressViewDelegate>
@property (strong, nonatomic) NSString *areaValue;
@property (strong, nonatomic) NSString*cityValue;
@property (strong, nonatomic) HZAreaPickerView *locatePicker;
@property (nonatomic,retain)NSString*provinceLabel;
@property (nonatomic,retain)NSString*cityLabel;
@property (nonatomic,retain)NSString*districtLabel;
-(void)cancelLocatePicker;
@end

@implementation AddOrEditAddressViewController

{

    NSString*isDefaultStr;
    AddAddressView *AddressView;

}
@synthesize alt = alt;
@synthesize areaValue=_areaValue, cityValue=_cityValue;
@synthesize locatePicker=_locatePicker;

-(void)setAreaValue:(NSString *)areaValue
{
    if (![_areaValue isEqualToString:areaValue]) {
        
        AddressView. regionTextField.text = areaValue;

    }
}

-(void)setCityValue:(NSString *)cityValue
{
    if (![_cityValue isEqualToString:cityValue]) {
       AddressView. regionTextField.text = cityValue;
    }
}

#pragma mark - HZAreaPicker delegate
-(void)pickerDidChaneStatus:(HZAreaPickerView *)picker
{
    if (picker.pickerStyle == HZAreaPickerWithStateAndCityAndDistrict) {
        self.areaValue = [NSString stringWithFormat:@"%@ %@ %@", picker.locate.state, picker.locate.city, picker.locate.district];
        self.provinceLabel= picker.locate.state;
        self.cityLabel=picker.locate.city;
        self.districtLabel=picker.locate.district;
        
        
        
    } else{
        self.cityValue = [NSString stringWithFormat:@"%@ %@", picker.locate.state, picker.locate.city];
        
    }
}
-(void)area:(NSString*)str{
    
    
    [AddressView.addressTextField resignFirstResponder];
    [AddressView. userNameTextField resignFirstResponder];
    [AddressView. telTextField resignFirstResponder];
    [AddressView. regionTextField resignFirstResponder];
    
    
    
    [self cancelLocatePicker];
    self.locatePicker = [[HZAreaPickerView alloc] initWithStyle:HZAreaPickerWithStateAndCityAndDistrict delegate:self] ;
    [self.locatePicker showInView:self.view];
    if ([self.editType isEqualToString:@"A"]) {

    AddressView.regionTextField.text=@"北京 通州";
    self.provinceLabel=@"北京";
    self.cityLabel=@"通州";
    }
    
}

-(void)cancelLocatePicker
{
    [self.locatePicker cancelPicker];
    self.locatePicker.delegate = nil;
    self.locatePicker = nil;
}

-(void)cancelLocatePicker:(NSString*)str{
 [self cancelLocatePicker];
}
#pragma mark - TextField delegate

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self cancelLocatePicker];
}

-(void)BackAction:(UIButton *)sender{
    
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidDisappear:(BOOL)animated

{
    
    [super viewDidDisappear:animated];
    
    self.navigationController.delegate = nil;
    
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
   }
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"新增收货地址";
    [self setNavigation];
    
    AddressView = [[AddAddressView alloc]initWithFrame:self.view.bounds];

    if ([self.editType isEqualToString:@"U"]) {
        isDefaultStr=[NSString stringWithFormat:@"%@", self.isDefault];
        AddressView = [[AddAddressView alloc]initWithFrame:self.view.bounds];
        AddressView.userNameTextField.text=self.name;
        AddressView.telTextField.text=self.phone;
        if ([isDefaultStr isEqualToString:@"1"]) {
             [AddressView.chooseBtn setImage:[UIImage imageNamed:@"jizhumima_hight"] forState:UIControlStateNormal];
        }else{
            [AddressView.chooseBtn setImage:[UIImage imageNamed:@"jizhumima_normal"] forState:UIControlStateNormal];}
        if (![ self. provinceName isEqual:[NSNull null]]) {
            self.provinceLabel= self. provinceName;
            self.cityLabel=self. cityName;
            self.districtLabel=self.regionName;
            AddressView. regionTextField.text=[NSString stringWithFormat:@"%@,%@,%@",   self. provinceName,self. cityName,self.regionName];
        }
        
        AddressView.addressTextField.text=self.address;
        
        [self.view addSubview:AddressView];

    }else{
     
   
    
    }
AddressView.mDelegate=self;
 [self.view addSubview:AddressView];
}
- (void)isDefaultStr:(NSString *)str
{
   
isDefaultStr=str;
    
    
}



-(void)ture:(NSString*)str{
    
    if ([AddressView.userNameTextField.text length] < 1) {
        [self showAlertMessage:@"请输入姓名"];
    } else{
        if ([AddressView.telTextField.text length] < 1)
        {
            [self showAlertMessage:@"请输入会员手机号码"];
        }
        else
        {
            if ([self.provinceLabel length] < 1)
            {
                [self showAlertMessage:@"请选择地区"];
            }
            else
            {
            NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
            NSString* Token = [user objectForKey:@"tokenId"];
            NSMutableDictionary *params = [NSMutableDictionary new];
            [params setValue:Token forKey:@"tokenId"];
            [params setValue:self.editType forKey:@"editType"];
            [params setValue:self.addressId forKey:@"addressId"];
            [params setValue:self.memberId forKey:@"memberId"];
            [params setValue:isDefaultStr forKey:@"isDefault"];
            [params setValue:self.provinceLabel forKey:@"provinceName"];
            [params setValue:self.cityLabel forKey:@"cityName"];
            [params setValue:self.districtLabel forKey:@"regionName"];
            [params setValue:AddressView.userNameTextField.text forKey:@"name"];
            [params setValue:AddressView.telTextField.text forKey:@"phone"];
            [params setValue:AddressView.addressTextField.text forKey:@"address"];
            NSLog(@"33333333====%@",params);
            [HttpRequest requestData:@"Business/Member/editAddress" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
                if (isSuccess) {
                    
                    NSLog(@"33333333====%@",severDataDict.dataDict);
                    
                    if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                        [Global hideProgressHUDInternal:self.view];
                  
                         [self.navigationController popViewControllerAnimated:YES];
                
                        
                    }else{
                 
                        NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                        
                        [Global showTextHud:requestResultStr view:self.view];
                        if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==9999) {
                 
                            
                            TheLoginViewController* Login=[[TheLoginViewController alloc]init];
                            [self.navigationController pushViewController:Login animated:YES];
                            
                        }
                    }
                }
                else
                {
                    [Global hideProgressHUDInternal:self.view];
                    
                    [Global showTextHudSub:severDataDict.resultStr view:self.view];
                }
                
            }];
            
            }}
    }
}
- (void)showAlertWait
{
    alt = [[UIAlertView alloc] initWithTitle:@"网络不稳定哦 ！" message:@"请确认网络" delegate:self cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
    
}



- (void)showAlertMessage:(NSString *)msg
{
    alt = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles: nil];
    [alt show];
    
    [self performSelector:@selector(hideAlert) withObject:nil afterDelay:1.0f];
}
- (void)hideAlert
{
    if (alt != nil)
    {
        [alt dismissWithClickedButtonIndex:0 animated:YES];
        alt = nil;
    }
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}





-(void)setNavigation{
    self.navigationController.navigationBar.barStyle = UIBaselineAdjustmentNone;
    UILabel *lineLabel=[[UILabel alloc] initWithFrame:CGRectMake(0, 43,ScreenWidth, 1)];
    [lineLabel setBackgroundColor:[UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1.0]];
    [self.navigationController.navigationBar addSubview:lineLabel];
    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];
    [[UINavigationBar appearance] setBarTintColor:Color];
    
    UIImage* backImage = [UIImage imageNamed:@"fanhui"];
    CGRect backframe = CGRectMake(0,0,30,30);
    UIButton* backButton= [[UIButton alloc] initWithFrame:backframe];
    [backButton setBackgroundImage:backImage forState:UIControlStateNormal];
    backButton.titleLabel.font=[UIFont systemFontOfSize:13];
    [backButton addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    self.navigationItem.leftBarButtonItem = leftBarButtonItem;
    self.navigationController.delegate = self;
    
    self.navigationController.interactivePopGestureRecognizer.delegate = self;
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@""] forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.shadowImage =[[UIImage alloc] init];
    
    [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:
                                                                     [UIColor colorWithRed:0/255.0 green:0/255.0 blue:0/255.0 alpha:1.0],
                                                                     UITextAttributeTextColor,
                                                                     [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1],
                                                                     UITextAttributeTextShadowColor,
                                                                     [NSValue valueWithUIOffset:UIOffsetMake(0, 0)],
                                                                     UITextAttributeTextShadowOffset,
                                                                     [UIFont fontWithName:@"Helvetica Neue" size:tetlefot],
                                                                     UITextAttributeFont,nil]];


}


@end
